﻿using FreeSql;

using GCC.Container.Abstracts;

namespace GCC.APIContainer
{
    /// <summary>
    /// 数据库管理器
    /// </summary>
    public class DBManager : IDBManager, IDisposable
    {
        /// <summary>
        /// 单例实例
        /// </summary>
        public static readonly DBManager Instance = new();
        private IFreeSql? _localDB;
        private IFreeSql? _localReadDB;
        private IFreeSql? _globalDB;
        private IFreeSql? _globalReadDB;

        private DBManager()
        {

        }

        internal void Init(int globalDBType, int localDBType, string globalDB, string globalReadDB, string localDB, string localReadDB)
        {

            {
                var dbStr = globalDB;
                var dbReadStr = globalReadDB;
                var dbType = (DataType)globalDBType;
                if (!string.IsNullOrWhiteSpace(dbStr))
                {
                    try
                    {
                        _globalDB = new FreeSqlBuilder().UseConnectionString(dbType, dbStr).UseExitAutoDisposePool(false).Build();
                    }
                    catch
                    {
                        _globalDB = null;
                    }
                }
                if (!string.IsNullOrWhiteSpace(dbReadStr))
                {
                    try
                    {
                        _globalReadDB = new FreeSqlBuilder().UseConnectionString(dbType, dbReadStr).UseExitAutoDisposePool(false).Build();
                    }
                    catch
                    {
                        _globalReadDB = null;
                    }
                }
            }



            {
                var dbStr = localDB;
                var dbReadStr = localReadDB;
                var dbType = (DataType)localDBType;
                if (!string.IsNullOrWhiteSpace(dbStr))
                {
                    try
                    {
                        _localDB = new FreeSqlBuilder().UseConnectionString(dbType, dbStr).UseExitAutoDisposePool(false).Build();
                    }
                    catch
                    {
                        _localDB = null;
                    }
                }
                if (!string.IsNullOrWhiteSpace(dbReadStr))
                {
                    try
                    {
                        _localReadDB = new FreeSqlBuilder().UseConnectionString(dbType, dbReadStr).UseExitAutoDisposePool(false).Build();
                    }
                    catch
                    {
                        _localReadDB = null;
                    }
                }

            }

#if DEBUG
            if (_globalDB is not null)
            {
                _globalDB.Aop.CurdAfter += (s, e) =>
                {
                    Console.WriteLine(e.Sql);
                };
            }
            if (_globalReadDB is not null)
            {
                _globalReadDB.Aop.CurdAfter += (s, e) =>
                {
                    Console.WriteLine(e.Sql);
                };
            }
            if (_localDB is not null)
            {
                _localDB.Aop.CurdAfter += (s, e) =>
                {
                    Console.WriteLine(e.Sql);
                };
            }
            if (_localReadDB is not null)
            {
                _localReadDB.Aop.CurdAfter += (s, e) =>
                {
                    Console.WriteLine(e.Sql);
                };
            }
#endif 
        }

        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            _localDB?.Dispose();
            _localReadDB?.Dispose();
            _globalDB?.Dispose();
            _globalReadDB?.Dispose();
            GC.SuppressFinalize(this);
        }

        IFreeSql? IDBManager.LocalDB { get => _localDB; }
        IFreeSql? IDBManager.LocalReadDB { get => _localReadDB; }
        IFreeSql? IDBManager.GlobalDB { get => _globalDB; }
        IFreeSql? IDBManager.GlobalReadDB { get => _globalReadDB; }
    }
}
